//
//  LjcDataBaseHandle.m
//  UI_元旦豆瓣电影
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LjcDataBaseHandle.h"
#import "ActivityModel.h"

@interface LjcDataBaseHandle ()

@property (nonatomic, copy) NSString *str0;
@end

@implementation LjcDataBaseHandle

- (void)dealloc {
    [_str0 release];
    [super dealloc];
}
// hahah
+ (LjcDataBaseHandle *)sharedDataBaseHandle {
    static LjcDataBaseHandle *handle = nil;
  
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[LjcDataBaseHandle alloc] init];
    });
    return handle;
}

- (void)openDB {
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *DBPath = [document stringByAppendingPathComponent:@"doubanMovie.sqlite"];
    NSLog(@"%@", DBPath);
    int result = sqlite3_open(DBPath.UTF8String, &sqPiont);
    if (result == SQLITE_OK) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
    
}

- (void)createActivityTable:(NSString *)str {
    self.str0 = str;
    NSString *sql = [NSString stringWithFormat:@"create table if not exists %@(number integer primary key autoincrement, title text)", _str0];
    
    int result = sqlite3_exec(sqPiont, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
}

- (void)insertTable:(ActivityModel *)model {
    NSString *sql = [NSString stringWithFormat:@"insert into %@(title) values('%@')", _str0, model.title];
    int result = sqlite3_exec(sqPiont, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
}

- (void)deleteTable:(ActivityModel *)model {
    NSString *sql = [NSString stringWithFormat:@"delete from Activity"];
    int result = sqlite3_exec(sqPiont, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
}

- (void)updataTable:(ActivityModel *)model {
    NSString *sql = [NSString stringWithFormat:@"update %@ set title = '0' where title = '%@'", _str0, model.title];
    int result = sqlite3_exec(sqPiont, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"yes");
    } else {
        NSLog(@"no");
    }
}

- (NSMutableArray *)selectTable {
    NSString *sql = [NSString stringWithFormat:@"select * from Activity"];
    
    sqlite3_stmt *stmt = nil;
    NSMutableArray *arr = [NSMutableArray array];
    int result = sqlite3_prepare_v2(sqPiont, sql.UTF8String, -1, &stmt, NULL);
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            ActivityModel *model = [ActivityModel activityModel];
            model.title = [NSString stringWithUTF8String:(const char*)title];
        
            [arr addObject:model];
        }
    } else {
        NSLog(@"查看no");
    }
    
    return arr;
}

@end
